package com.digitaleye.dpslite;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.downpayment.dps.R;

public class TipsAndRemindersActivity extends Activity {
	
	
	TipsDBHelper tipsDBHelper;
	
	WebHelper webHelper;
	
	DBHelper dbHelper;
	
	/**Layout containing the Tips Table **/
	TableLayout tipsTable = null;
	
	SharedPreferences prefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.webHelper = new WebHelper(this);
		this.dbHelper = new DBHelper(this);
		this.tipsDBHelper = new TipsDBHelper(this);
		setContentView(R.layout.savings_tips_tab_layout);
		
		this.prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		
		addOnClickListeners();
		
		addSavingsTips();
		
		
		
		//createFrequencySpinner();
		
		//addCheckBoxListeners();
		
		//addEmailTextListener();
		
//		EditText emailText = (EditText) findViewById(R.id.emailText);
//		prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//		
//		if(prefs.getBoolean("enableEmailPref", false) == true){
//			emailText.setVisibility(View.VISIBLE);
//		}
		
		
		 
		
		
	}
	
	@Override
	public void onResume(){
		
		try{
			super.onResume();
			
			addSavingsTips();
		}
		catch(Exception e){
			
		}
		
	}
	
	
	
	public void addSavingsTips(){
		
		LayoutInflater layoutInflater = (LayoutInflater) 
		        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		
		
		/** inflate tips box **/
		LinearLayout tipsBox1 = (LinearLayout) findViewById(R.id.tipsBox1);
		LinearLayout tipsBox2 = (LinearLayout) findViewById(R.id.tipsBox2);
		LinearLayout tipsBox3 = (LinearLayout) findViewById(R.id.tipsBox3);
		//LinearLayout tipsBox1 = (LinearLayout) findViewById(R.id.tipsBox1);
		//LinearLayout tipsBox1 = (LinearLayout) findViewById(R.id.tipsBox1);
		
		tipsBox1.removeAllViews();
		tipsBox2.removeAllViews();
		tipsBox3.removeAllViews();
		
		tipsBox1.addView(layoutInflater.inflate(R.layout.savings_tips_box, tipsBox1, false ));
		tipsBox2.addView(layoutInflater.inflate(R.layout.savings_tips_box, tipsBox2, false ));
		tipsBox3.addView(layoutInflater.inflate(R.layout.savings_tips_box, tipsBox3, false ));
		
		int tipID = Utility.randInt(1, tipsDBHelper.tableSize-1);
		/** add tips content**/
		
		
		//pick 3 random unique Integers
		List<Integer> tipIDList = new ArrayList<Integer>();
		
		for(int i = 1; i < tipsDBHelper.tableSize-1; i++){
			tipIDList.add(i);
		}
		Collections.shuffle(tipIDList, new Random());
		
		Log.e("chosen tips = ", "chosen tips = " + tipIDList.get(0) + " " + tipIDList.get(1) + " " +tipIDList.get(2));
		
		
		Tip tip = this.tipsDBHelper.getTip(tipIDList.get(0));
		Log.e("tip" , tip.text);
		TextView tipText = (TextView) tipsBox1.findViewById(R.id.tipText);
		tipText.setText(Html.fromHtml(tip.text));
		
		tip = this.tipsDBHelper.getTip(tipIDList.get(1));
		Log.e("tip" , tip.text);
		tipText = (TextView) tipsBox2.findViewById(R.id.tipText);
		tipText.setText(Html.fromHtml(tip.text));
		
		tip = this.tipsDBHelper.getTip(tipIDList.get(2));
		Log.e("tip" , tip.text);
		tipText = (TextView) tipsBox3.findViewById(R.id.tipText);
		tipText.setText(Html.fromHtml(tip.text));
		
		
		
		
//		
//		/** Insert Tips Table **/
//		this.tipsTable = (TableLayout) findViewById(R.id.tipsTable);
//		
//		this.tipsTable.removeAllViews();
//		
//		
//		
//		
//		tipsTable.addView(layoutInflater.inflate(R.layout.tips_layout, tipsTable, false )); 
//		/**********************/
//		
//		TextView titleText = (TextView) this.tipsTable.findViewById(R.id.tipTitle);
//		
//		tipText = (TextView) this.tipsTable.findViewById(R.id.tipText);
//		
//		Tip tip = this.tipsDBHelper.getTip();
//		
//		if(tip != null){
//			titleText.setText(tip.title);
//			tipText.setText(tip.text);
//		}
		

		
	}
	
	
	
	public void addOnClickListeners(){
		
		
		ImageView okayButton = (ImageView) findViewById(R.id.okay_button);
		
		okayButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				
				
				EditText emailText = (EditText) findViewById(R.id.emailText);
				
				if(emailText.getText().toString().length() <= 0){
					Toast.makeText(TipsAndRemindersActivity.this, "Please enter an email address", Toast.LENGTH_LONG).show();
				}
				else if (!Utility.validEmail(emailText.getText().toString())){
					Toast.makeText(TipsAndRemindersActivity.this, "The email address you entered is not valid.", Toast.LENGTH_LONG).show();
				}
				
				/** TODO subscribe to email **/
				else {
					//Toast.makeText(TipsAndRemindersActivity.this, "subscribed", Toast.LENGTH_LONG).show();
					
					webHelper.subscribe(emailText.getText().toString());
					
				}
				
				
				
				
			}
			
		});
		
		
		okayButton.setOnTouchListener(new OnTouchListener(){
			

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				ImageView image = (ImageView) v;
				
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					image.setImageDrawable(v.getResources().getDrawable(R.drawable.okay_btn_clicked));

				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					image.setImageDrawable(v.getResources().getDrawable(R.drawable.okay_btn));
				}
				else if(event.getAction() == MotionEvent.ACTION_CANCEL){
					image.setImageDrawable(v.getResources().getDrawable(R.drawable.okay_btn));
				}
				
				return false;
			}

		});

		
	}
	

}
